package com.ztmg.water.service.order.impl;

import com.ztmg.common.util.DateUtil;
import com.ztmg.water.dto.base.DeliveryManDTO;
import com.ztmg.water.dto.bills.OutPutBillsDTO;
import com.ztmg.water.dto.order.OrderPayDTO;
import com.ztmg.water.mapper.bills.OutPutBillsMapper;
import com.ztmg.water.mapper.student.StudentMapper;
import com.ztmg.water.mapper.student.StudentTicketMapper;
import com.ztmg.water.mapper.water.WaterMapper;
import com.ztmg.water.mapper.water.WaterTicketMapper;
import com.ztmg.water.model.bills.OutPutBills;
import com.ztmg.water.model.order.OrderWater;
import com.ztmg.water.model.student.Student;
import com.ztmg.water.model.student.StudentTicket;
import com.ztmg.water.model.student.StudentTicketExample;
import com.ztmg.water.model.water.Water;
import com.ztmg.water.model.water.WaterTicket;
import com.ztmg.water.service.student.StudentService;
import com.ztmg.water.util.RandomUtil;
import com.ztmg.water.vo.order.OrderWaterVO;
import com.ztmg.water.dto.order.OrderWaterDTO;
import com.ztmg.water.dto.order.OrderWaterPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.order.OrderWaterService;
import com.ztmg.water.mapper.order.OrderWaterMapper;import com.ztmg.water.model.order.OrderWaterExample;

@Service
public class OrderWaterServiceImpl implements OrderWaterService{

    @Resource
    private OrderWaterMapper orderWaterMapper;
    @Resource
    private WaterMapper waterMapper;
    @Resource
    private StudentTicketMapper studentTicketMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private OutPutBillsMapper outPutBillsMapper;

    @Override
    public JsonResult selectCountOrderListByDeliveryMan(DeliveryManDTO dto) {
        int nums = orderWaterMapper.selectCountOrderListByDeliveryMan(dto);
        return JsonResult.success(nums);
    }

    @Override
    public JsonResult<List<OrderWaterVO>> selectOrderListByOrderID(OutPutBillsDTO dto) {
        /***
         * 根据具体的业务，查询条件赋值
         */
        OutPutBills bills  = outPutBillsMapper.selectByPrimaryKey(dto.getId());


        String[] orderArray = bills.getOrderIds().split(",");
        StringBuilder orderIds = new StringBuilder();
        orderIds.append("(");
        List<OrderWaterVO> respDTOS = new ArrayList<>();
        if(!orderArray[0].equals("") && orderArray.length>0)
        {
            for (int i=0;i<orderArray.length;i++)
            {
                orderIds.append("\"");
                orderIds.append(orderArray[i]);
                orderIds.append("\"");
                orderIds.append(",");
            }
            orderIds.delete(orderIds.length()-1,orderIds.length());
            orderIds.append(")");
            dto.setOrderIds(orderIds.toString());
            respDTOS = orderWaterMapper.selectOrderListByOrderId(dto);

        }
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<List<OrderWaterVO>> selectOrderListByDeliveryMan(DeliveryManDTO dto) {

        /***
         * 根据具体的业务，查询条件赋值
         */
        List<OrderWaterVO> sysUserList = orderWaterMapper.selectOrderListByDeliveryMan(dto);
        return JsonResult.success(sysUserList);
    }

    @Override
    public JsonResult create(OrderWaterDTO dto) {
        if(null != dto){
            Water water = waterMapper.selectByPrimaryKey(dto.getWaterId());
            Student student = studentMapper.selectByPrimaryKey(dto.getStudentId());
            OrderWater bean = new OrderWater();
            BeanUtils.copyProperties(dto,bean);
            bean.setWaterPrice(water.getWaterPrice());
            bean.setOrderSn(this.getOrderSn());
            bean.setCreateTime(DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
            bean.setStudentName(student.getStudentName());
            bean.setStudentAdder(student.getStudentAdder());
            bean.setPlaceOrderTime(DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
            bean.setStatus(1);
            //后续完善
            bean.setCreater("");
            orderWaterMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(OrderWaterDTO dto) {
        if(null != dto && null != dto.getId()){
            OrderWater bean = new OrderWater();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
            //后续完善
            bean.setModifier("");
            orderWaterMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<OrderWaterVO> selectById(Integer id) {
        if(null != id){
            OrderWater bean = orderWaterMapper.selectByPrimaryKey(id);
            OrderWaterVO vo = new OrderWaterVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<OrderWaterVO> select(OrderWaterDTO dto) {
        OrderWaterExample example = new OrderWaterExample();
        OrderWaterExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<OrderWater> sysUserList = orderWaterMapper.selectByExample(example);
        OrderWaterVO vo = new OrderWaterVO();
        if(!sysUserList.isEmpty()){
            BeanUtils.copyProperties(sysUserList.get(0),vo);
        }
        return JsonResult.success(vo);
    }

    @Override
    public JsonResult<PageInfo<OrderWaterVO>> selectPage(OrderWaterPageDTO dto) {
        OrderWaterExample example = new OrderWaterExample();
        OrderWaterExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<OrderWater> beanList = orderWaterMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<OrderWaterVO> respDTOS = beanList.parallelStream().map((bean) -> {
            OrderWaterVO vo = new OrderWaterVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( orderWaterMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(OrderWaterDTO dto) {
        OrderWaterExample example = new OrderWaterExample();
        OrderWaterExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(orderWaterMapper.deleteByExample(example));
    }

    @Override
    public JsonResult toPay(OrderPayDTO dto) {
        OrderWaterExample example = new OrderWaterExample();
        OrderWaterExample.Criteria criteria =  example.createCriteria();
        OrderWater orderWater = orderWaterMapper.selectByPrimaryKey(dto.getOrderId());
       //水票支付
        if(dto.getPayType() == 2){
            StudentTicketExample example1 = new StudentTicketExample();
            example1.createCriteria().andStudentIdEqualTo(orderWater.getStudentId()).andTicketIdEqualTo(dto.getTicketId());
           List<StudentTicket> studentTickets =  studentTicketMapper.selectByExample(example1);
           if(!studentTickets.isEmpty()){
               StudentTicket studentTicket = studentTickets.get(0);
               if(studentTicket.getTicketNum()>= dto.getTicketNum()){
                   studentTicket.setTicketNum(studentTicket.getTicketNum()-dto.getTicketNum());
                   studentTicketMapper.updateByPrimaryKeySelective(studentTicket);
               }else{
                   return JsonResult.fail("水票不足，请重新支付!");
               }
           }else{
               return JsonResult.fail("水票信息有误!");
           }

        }else{
            //微信支付
        }
        orderWater.setPayTime(DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
        orderWater.setStatus(2);
        orderWater.setModifyTime(DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
        return JsonResult.success();
    }

    private String getOrderSn(){
        String dateStr = DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYYMMDDHHMISS);
        return dateStr+ RandomUtil.getRandom(4);
    }
}